package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.AllItemResponse;
import com.cskaoyan.mall.dto.ProductDetailRequest;
import com.cskaoyan.mall.dto.ProductDetailResponse;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.AllProductRequest;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.dto.RecommendResponse;
import com.cskaoyan.shopping.service.IProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Set;

/**
 * @author LC_RSH
 * @date 2022/7/9 15:17
 */
@Slf4j
@Service
public class IProductServiceImpl implements IProductService {
    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ContentConverter contentConverter;

    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        return null;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        return null;
    }

    @Override
    public RecommendResponse getRecommendGoods() {

        RecommendResponse response = new RecommendResponse();
        try {
            // 热门推荐的id为6
            List<Panel> panels = panelMapper.selectPanelContentById(6);
            for (Panel panel : panels) {
                panel.setPanelContentItems(panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId()));
            }
            Set<PanelDto> panelDtos = contentConverter.panels2PanelDto(panels);
            // 设置成功状态码
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            // 设置成功信息
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            // 设置返回数据
            response.setPanelContentItemDtos(panelDtos);
        } catch (Exception e) {
            log.error("IProductServiceImpl.getRecommendGoods Occur Exception"+e);

            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }

    @Override
    public AllItemResponse getAllItems() {
        return null;
    }
}

/**
 * @author 86157
 * @since 2022/07/09 15:17
 */